package com.technology.education.ui.teacher.mine

import android.os.Bundle
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.os.bundleOf
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.LinearLayoutManager
import com.angcyo.dsladapter.*
import com.google.android.material.imageview.ShapeableImageView
import com.technology.education.R
import com.technology.education.data.AppData
import com.technology.education.data.SpKeys
import com.technology.education.data.model.UserInfoModel
import com.technology.education.databinding.FragmentTeacherMineBinding
import com.technology.education.dslitem.AppStatusItem
import com.technology.education.dslitem.ItemColumnTitleText
import com.technology.education.dslitem.style.DrawableStyle
import com.technology.education.ext.*
import me.hgj.jetpackmvvm.base.fragment.BaseVmDbFragment

class MineFragment : BaseVmDbFragment<MineViewModel, FragmentTeacherMineBinding>() {
    /**提供悬停功能*/
    var hoverItemDecoration = HoverItemDecoration()

    /**提供基本的分割线功能*/
    var baseDslItemDecoration = DslItemDecoration()

    var myAdapter: DslAdapter = DslAdapter().apply {
        dslAdapterStatusItem = AppStatusItem()
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.toolBar.toolbar.title = "我的"
        mDatabind.toolBar.toolbar.navigationIcon = null
        initVertical()
    }

    /**
     * 初始化为垂直列表
     */
    private fun initVertical() {
        mDatabind.rvList.apply {
            addItemDecoration(baseDslItemDecoration)
            hoverItemDecoration.attachToRecyclerView(this)
            setHasFixedSize(true)
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            adapter = myAdapter
            setItemViewCacheSize(20)
        }
    }

    override fun lazyLoadData() {
    }

    override fun createObserver() {
        getAppViewModel().userInfo.observe(this) {
            setUI(it)
        }
        observe(SpKeys.UPDATE_USER_AVATAR, String::class.java) {
            myAdapter.render {
                updateItem("info")
            }
        }
        observe(SpKeys.UPLOAD_AUTHOR, Boolean::class.java) {
            if (it) {
                myAdapter.updateItem("author")
            }
        }
    }

    override fun showLoading(message: String) {
    }

    override fun dismissLoading() {
    }

    private fun setUI(info: UserInfoModel) {
        myAdapter.render {
            clearAllItems()
            dslItem(R.layout.item_mine_info) {
                itemTag = "info"
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemHolder.tv(R.id.tv_name)?.apply {
                        text = info.name
                    }
                    itemHolder.tv(R.id.tv_phone)?.text = info.mobile
                    itemHolder.v<ImageView>(R.id.iv_header)?.apply {
                        header(info.avatar, this)
                    }
                    itemHolder.clickItem {
                        navigation(it, R.id.action_mine_to_manage_account)
//                        videoCall("22222", "16", 33333)
                    }
                }
            }

            dslItem(R.layout.item_mine_time_remain) {
                itemTag = "time"
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemHolder.tv(R.id.tv_time_remain)?.text = info.balanceDuration.toString()
                    itemHolder.click(R.id.tv_time_record) {
                        navigation(it, R.id.action_mine_to_score_record)
                    }
                    itemHolder.tv(R.id.tv_pay)?.text = "兑换"
                    itemHolder.click(R.id.tv_pay) {
                        navigation(it, R.id.action_mine_to_withdraw, bundleOf("type" to 4))
                    }
                }
            }
            dslItem(R.layout.item_student_mine_populaize) {
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemTopInsert = 10 * dpi
                    itemBottomInsert = 10 * dpi
                    itemHolder.clickItem {
                        navigation(it, R.id.action_mine_to_popularize)
                    }
                }
            }
            this + ItemColumnTitleText(
                "保障金管理",
                info.depositStateStr,
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.verify),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemTag = "manage"
                style.contentTextColor =
                    if (info.depositState == "0") R.color.text_h else R.color.theme_color
                itemClick = {
                    navigation(it, R.id.action_mine_to_deposit)
                }
            } + ItemColumnTitleText(
                "资质认证",
                info.certString,
                tag="author",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.person),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemTag="author"
                itemClick = {
                    if (info.certState == "1") {
                        toast("您已通过资质认证")
                    } else {
                        navigation(it, R.id.action_mine_to_authorFragment)
                    }

                }
            } + ItemColumnTitleText(
                "设置",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.setting),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemClick = {
                    navigation(it, R.id.action_mine_to_setting)
                }
            } + ItemColumnTitleText(
                "联系客服",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.icon_heart),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemClick = {
                    callPhoneDial(getAppViewModel().userInfo.value?.servicePhone)
                }
            } + ItemColumnTitleText(
                "意见反馈",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.icon_mine_advice),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemClick = {
                    navigation(it, DEEP_LINK_FEED_BACK)
                }
            } + ItemColumnTitleText(
                "辅导员管理规范",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.note),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemClick = {
                    toWebActivity("管理规范", AppData.MANAGE_PROTOCOL)
                }
            }
        }
    }

}